
package com.example.mq;

import com.example.api.config.OtherConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;


/**
 * 消息队列配置
 *
 * @author lr
 * @create 2021/9/16
 */

@Configuration
@Slf4j
public class TopicRabbitConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {

    @Resource
    private OtherConfig otherConfig;

    @Resource
    private RabbitTemplate rabbitTemplate;


    @Bean
    public Queue projectCostQueue() {
        return new Queue(otherConfig.getQueue());
    }


    /**
     * 定义交换机
     */

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(otherConfig.getExchange());
    }


    /**
     * 绑定消息队列到交换机
     *
     * @return
     */

    @Bean
    Binding bindingExchangeProjectCost() {
        return BindingBuilder.bind(projectCostQueue()).to(exchange()).with(otherConfig.getProjectMessage());
    }

    /**
     * 构造方法之后执行
     */
    @PostConstruct
    public void init() {
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);

    }


    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("消息标识：" + correlationData);
        log.info("是否到达交换机：" + ack);
        log.info("失败原因：" + cause);
        if (ack) {
            log.info("消息发送成功");
        } else {
            //如果发送失败业务处理
            log.info("消息发送到exchange失败,原因: {}", cause);
        }
    }

    @Override
    public void returnedMessage(Message message, int i, String s, String s1, String s2) {
        System.out.println("消息未到达队列");
    }

    /**
     * 设置消息可靠性传递
     *
     * @param connectionFactory
     * @return
     */
//    @Bean
//    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
//    public RabbitTemplate rabbitTemplates(ConnectionFactory connectionFactory) {
//        RabbitTemplate rabbitTemplate = new RabbitTemplate((connectionFactory) );
//        //消息发送到交换器时回调
//        rabbitTemplate.setConfirmCallback((correlationData, ack, messages) -> {
//            if (ack) {
//                redisTemplate.delete(correlationData.getId());
//            } else {
//                //如果发送失败业务处理
//                log.info("消息发送到exchange失败,原因: {}", messages);
//            }
//        });
//        //消息发送到交换器，但无队列与交换器绑定时回调；
//        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
//            String correlationId = message.getMessageProperties().getCorrelationId();
//            log.debug("消息：{} 发送失败, 应答码：{} 原因：{} 交换机: {}  路由键: {}", correlationId, replyCode, replyText, exchange, routingKey);
//        });
//
//
//        return rabbitTemplate;
//    }
}
